<?php
/* System Class
 * Adam Malevski
*/

// 0 - isjungtas; 1 - ijungtas
$DEBUG = 0;
class System {

	function __construct() {
	}

	// $allowed masyve IP adresai kurie gali debugint
	function canDebug() {
		global $DEBUG;
		$allowed = array ('127.0.0.1', '193.219.42.119', '84.32.47.117');
		if (in_array ($_SERVER['REMOTE_ADDR'], $allowed)) return $DEBUG;
		else return 0;
	}
	
	// debuginimo funkcija
	function debug($message) {
		if (!$this->canDebug()) return;
		echo '<div style="background:yellow; color:black; border: 1px solid black;';
		echo 'padding: 5px; margin: 5px; white-space: pre;">';
		if (is_string ($message)) echo $message;
		else var_dump ($message);
		echo '</div>';
	}
	
	function getFirstImage($dirname) 
	{ 
		global $imageName; 
		$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF", "JPEG"); 
		if($handle = opendir($dirname)) 
			{ 
			$i = 0;
				while(false!== ($file = readdir($handle))) 
				{ 
					if(strstr($file, "." . $ext[$i])) 
					{ 
						$imageName = $file; 
						break; 
					} 
				} 
			closedir($handle); 
			} 
		return($imageName); 
	}
	
	function GenerateUrl ($s) {
		$from = explode (',', "ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,(,),[,],'");
		$to = explode (',', 'c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,,,,,,');
		$s = preg_replace ('~[^\w\d]+~', '-', str_replace ($from, $to, trim ($s)));
		return strtolower (preg_replace ('/^-/', '', preg_replace ('/-$/', '', $s)));
	}
	
	function CheckUrl ($s) {
		$myurl = preg_replace ('/\?.*$/', '', $_SERVER['REQUEST_URI']);
		if ($myurl != "/$s") {Header ("Location: /$s", true, 301); exit;}
	}
	
	function limitWords($string, $lim) {
		$limit = (-1)*$lim;
		$ret = substr($string, 0, $limit);
		return $ret;
	}
	
	function fileExists($file) {
  	$ps = explode(":", ini_get('include_path'));
  		foreach($ps as $path)
  		{
  		  if(file_exists($path.'/'.$file)) return true;
  		}
  		if(file_exists($file)) return true;
  			return false;
	}
}
?>